package entity.airline.impl;

import entity.airline.Airline;
import entity.flight.Flight;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "airLine")
public class AirlineImpl implements Airline {

	private Long id;

	private String name;

	List<Flight> flights;

	public AirlineImpl(String name) {
		this.name = name;
		this.flights = new ArrayList<Flight>();
	}

	@NotEmpty
	@Column(name = "name")
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "airline")
	public List<Flight> getFlights() {
		return flights;
	}

	@Override
	public void setFlights(List<Flight> flights) {
		this.flights = flights;
	}

	@Override
	public void addFlight(Flight flight) {
		if (flight == null)
			this.flights = new ArrayList<Flight>();
		this.flights.add(flight);
	}

	public void removeFlight(Flight flight) {
		this.flights.remove(flight);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
